package cn.net.autocode.platform.roles;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.UserThreadLocalUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.SqlWrapperBuilder;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/platform/ugroup")
public class PlatformUgroupCtrl {

    private final Dao dao;

    public PlatformUgroupCtrl(Dao dao) {
        this.dao = dao;
    }

    @RequestMapping("selectUserNotInUgroup")
    public ResultT selectUserNotInUgroup(@RequestBody JSONObject param){
        JSONObject pagingData = param.getJSONObject("pagingData");
        JSONObject operData = param.getJSONObject("operData");
        String ugroupId = operData.getString("ugroupId");
        SqlWrapperBuilder swb = new SqlWrapperBuilder("sys_users t1,sys_organ_users t2");
        swb.selectColumn("t1.*");
        swb.where("t1.username = t2.username and t2.org_code=?",UserThreadLocalUtils.getOrgCode());
        swb.where("not exists(select t3.id from sys_ugroup_users t3 where t3.username=t1.username and t3.ugroup_id=?)",ugroupId);

        String keyWord = operData.getString("keyWord");
        if(keyWord != null && !keyWord.isBlank()){
            keyWord = "%"+keyWord.trim()+"%";
            swb.where("(t1.username like ? or t1.fullname like ?)",keyWord,keyWord);
        }

        JSONObject rs = dao.queryPageAndData(swb,pagingData);
        return ResultT.success(rs);
    }
}
